'use strict';

module.exports = {
    // 短信验证码缓存容器
    VERIFYCODE: {
        mark: '这是短信验证码缓存容器'
    },
    // 储存验证码方法
    async saveVerifyCode({ phone }) {
        // 60秒内不能重发
        if (this.VERIFYCODE[phone]) {
            return { status: false, verifyCode: this.VERIFYCODE[phone] };
        }
        // 缓存短信验证码
        let verifyCode = '';
        for (let i = 0; i < 6; i++) {
            verifyCode += Math.floor(Math.random() * 10);
        }
        this.VERIFYCODE[phone] = verifyCode;
        setTimeout(_ => delete this.VERIFYCODE[phone], 1000 * 60);
        return { status: true, verifyCode };
    },
    // 校验验证码方法
    async isVerifyCode({ phone, verifyCode }) {
        if (verifyCode === this.VERIFYCODE[phone]) {
            delete this.VERIFYCODE[phone];
            return true;
        }
        return false;
    }
};
